package com.sun.alen.service.Impl;

import com.sun.alen.entity.*;
import com.sun.alen.mapper.MainClassMapper;
import com.sun.alen.mapper.ProfessionMapper;
import com.sun.alen.mapper.StudentMapper;
import com.sun.alen.mapper.TeacherMapper;
import com.sun.alen.service.StudentService;
import com.sun.alen.utils.PhoneUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private MainClassMapper mainClassMapper;

    @Autowired
    private TeacherMapper teacherMapper;

    @Autowired
    private ProfessionMapper professionMapper;
    @Override
    public boolean add(Student student) {
        if(student.getStudentPassword().isEmpty()){
            student.setStudentPassword("123456");
        }
        return studentMapper.add(student);
    }

    @Override
    public boolean update(Student student) {
        return studentMapper.update(student);
    }

    @Override
    public boolean delete(String id) {
        return studentMapper.delete(id);
    }

    @Override
    public PageBean search(String input, String select, Integer currentPage, Integer pageSize) {
        Integer start = (currentPage - 1) * pageSize;
        int endIndex = 0;
        List<Student> studentList = new ArrayList<>();
        long count = 0;
        input = "%" + input + "%";
        if (input.equals("%%")) {
            studentList = studentMapper.list(start, pageSize);
            count = studentMapper.count();
        } else {
            switch (select) {
                case "1" -> {
                    studentList = studentMapper.searchByName(input, start, pageSize);
                    count = studentMapper.countByName(input);
                }
                case "2" -> {
                    studentList = studentMapper.searchBySex(input, start, pageSize);
                    count = studentMapper.countBySex(input);
                }
                case "3" -> {
                    studentList = studentMapper.searchByNativePlace(input, start, pageSize);
                    count = studentMapper.countByNativePlace(input);
                }
                case "4" -> {
                    List<MainClass> mainClassList = mainClassMapper.searchIdByName(input);
                    for (int i = 0; i < mainClassList.size(); i++) {
                        List<Student> tempList = studentMapper.searchByClass(mainClassList.get(i).getClassId());
                        studentList.addAll(tempList);
                    }
                    endIndex = Math.min(start + pageSize, studentList.size());
                    count = studentList.size();
                    studentList = studentList.subList(start,endIndex);
                }
                case "5" -> {
                    List<Teacher> teacherList = teacherMapper.searchIdByName(input);
                    for (int i = 0; i < teacherList.size(); i++) {
                        List<Student> tempList = studentMapper.searchByTeacher(teacherList.get(i).getTeacherId());
                        studentList.addAll(tempList);
                    }
                    endIndex = Math.min(start + pageSize, studentList.size());
                    count =studentList.size();
                    studentList = studentList.subList(start,endIndex);
                }
                case "6" -> {
                    List<Profession> professionList = professionMapper.searchIdByName(input);
                    for (int i = 0; i < professionList.size(); i++) {
                        List<Student> tempList = studentMapper.searchByProfession(professionList.get(i).getProfessionId());
                        studentList.addAll(tempList);
                    }
                    endIndex = Math.min(start + pageSize, studentList.size());
                    count = studentList.size();
                    studentList = studentList.subList(start,endIndex);
                }
                default -> {
                    studentList = studentMapper.list(start, pageSize);
                    count = studentMapper.count();
                }
            }
        }

        PageBean pageBean = new PageBean(count, studentList);
        return pageBean;
    }

    @Override
    public List<Student> all() {
        return studentMapper.all();
    }

    @Override
    public Student login(Student student) {
        return studentMapper.login(student);
    }

    @Override
    public int repass(String phone, String pass, String verify) {
        PhoneUtils phoneUtils=new PhoneUtils();
        boolean flag=phoneUtils.verifyCode(phone,verify);
        if(flag){
            boolean status=studentMapper.repass(phone,pass);
            if(status){
                return 0;
            }else {
                return 1;
            }
        }else {
            return 2;
        }
    }

    @Override
    public PageBean searchTeacher(String input, String select, Integer teacherId, Integer currentPage, Integer pageSize) {
        Integer start = (currentPage - 1) * pageSize;
        int endIndex = 0;
        List<Student> studentList=new ArrayList<>();
        long count=0;
        input="%"+input+"%";
        if(input.equals("%%")){
            studentList=studentMapper.listByTeacher(teacherId,start,pageSize);
            count=studentMapper.countByTeacher(teacherId);
        }else {
            switch (select){
                case "1"->{
                    List<Profession> professionList =professionMapper.searchByName(input,start,pageSize);
                    for (int i = 0; i < professionList.size(); i++) {
                        List<Student> tempList = studentMapper.searchByProfessionId(professionList.get(i).getProfessionId(),teacherId);
                        studentList.addAll(tempList);
                    }
                    endIndex = Math.min(start + pageSize, studentList.size());
                    count = studentList.size();
                    studentList = studentList.subList(start,endIndex);
                }
                case "2"->{
                    List<MainClass> mainClassList = mainClassMapper.searchByClassName1(input,teacherId);
                    for (int i = 0; i < mainClassList.size(); i++) {
                        List<Student> tempList = studentMapper.searchByClassId(mainClassList.get(i).getClassId(),teacherId);
                        studentList.addAll(tempList);
                    }
                    endIndex = Math.min(start + pageSize, studentList.size());
                    count = studentList.size();
                    studentList = studentList.subList(start,endIndex);
                }
                case "3"->{
                    studentList=studentMapper.searchByNameAndTeacher(input,teacherId,start,pageSize);
                    count=studentMapper.countByNameAndTeacher(input,teacherId);
                }
                default->{
                    studentList=studentMapper.listByTeacher(teacherId,start,pageSize);
                    count=studentMapper.countByTeacher(teacherId);
                }
            }
        }
        PageBean pageBean = new PageBean(count,studentList);
        return pageBean;
    }

    @Override
    public List<Student> getStudentByMain(String id) {
        return studentMapper.getStuByMain(id);
    }

    @Override
    public boolean updatePassword(Student student) {
        return studentMapper.updatePassword(student);
    }

    @Override
    public Student selectById(int studentId) {
        return studentMapper.selectById(studentId);
    }
}
